package com.scisoft.scan.control.depotoutsale;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ClearEditText;
import com.scisoft.scan.control.MainMenuActivity;
import com.scisoft.scan.data.depotoutsale.DOSOrderBean;
import com.scisoft.scan.data.depotoutsale.DOSOrderStatisBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_DepotOutSale;

import java.util.ArrayList;
import java.util.List;

public class DOSOrderActivity extends Activity {
    private ImageView iv_Back; //返回按钮
    private ImageView iv_query; //搜索按钮

    private SwipeRefreshLayout mRefreshLayout;
    private ListView lv_data;
    private TextView bt_undo;
    private TextView bt_done;
    private View v_undo;
    private View v_done;
    private ClearEditText et_bar;
    private String bizId;// 1 全部 2 未处理 3 处理中 4 已完成
    //全部的订单
    private List<DOSOrderBean> depotOrderBeanList;
    //当前过滤后的订单，列表操作的是这个
    private List<DOSOrderBean> filterDepotOrderBeanList;
    private DOSOrderStatisBean depotOrderStatis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//即隐藏标题栏
        setContentView(R.layout.activity_dosorder);

        //下拉刷新控件
        mRefreshLayout = findViewById(R.id.swipe_layout);
        mRefreshLayout.setProgressViewEndTarget(true, 0);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshLayout.setRefreshing(false);
                QueryDepotOrder(false);
            }
        });
        //控件初始化
        lv_data = findViewById(R.id.lv_data);
        lv_data.setOnItemClickListener(new ListViewClick());
        bt_undo = findViewById(R.id.bt_undo);
        bt_done = findViewById(R.id.bt_done);
        v_undo = findViewById(R.id.v_undo);
        v_done = findViewById(R.id.v_done);
        et_bar = findViewById(R.id.et_bar);
        et_bar.setText("");
        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DOSOrderActivity.this, MainMenuActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
        //搜索框
        iv_query = findViewById(R.id.iv_query);
        iv_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterData(et_bar.getUpperText().toString());
                BindData(DOSOrderActivity.this);
            }
        });
        //页签切换
        bt_undo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
            }
        });
        bt_done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
            }
        });

        setButton(bt_undo);
    }

    //设置tab页的颜色
    private void setButton(View v) {
        if (v.getId() == bt_undo.getId()) {
            bizId = "1";//未审核
            v_undo.setVisibility(View.VISIBLE);
            v_done.setVisibility(View.INVISIBLE);
            bt_undo.setTextColor(getResources().getColor(R.color.tab_text_color_select));
            bt_done.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
        } else {
            bizId = "2";//已审核
            v_undo.setVisibility(View.INVISIBLE);
            v_done.setVisibility(View.VISIBLE);
            bt_undo.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_done.setTextColor(getResources().getColor(R.color.tab_text_color_select));
        }
        QueryDepotOrder(false);
    }

    //进入二级界面
    private class ListViewClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(DOSOrderActivity.this, DOSOrderDetailActivity.class);
            if (position >= 0 && position < filterDepotOrderBeanList.size()) {
                DOSOrderBean depotOrderBean = filterDepotOrderBeanList.get(position);
                intent.putExtra("data", depotOrderBean);
                startActivityForResult(intent, 0);
            }
        }
    }

    //返回当前界面
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (bizId) {
            case "1":
                setButton(bt_undo);
                break;
            case "2":
                setButton(bt_done);
                break;
        }
    }

    //查询订单
    private void QueryDepotOrder(boolean depotChanged) {
        CommonUtil.showPress(DOSOrderActivity.this);
        QueryDepotOrderTask QueryPackInfoTask = new QueryDepotOrderTask(DOSOrderActivity.this, depotChanged);
        QueryPackInfoTask.execute();
    }

    //过滤(配件列表包含过滤条件)
    private void filterData(String filter) {
        filterDepotOrderBeanList = new ArrayList<>();
        if (filter != null && filter != "") {
            if (depotOrderBeanList != null) {
                for (int i = 0; i < depotOrderBeanList.size(); i++) {
                    DOSOrderBean bean = depotOrderBeanList.get(i);
                    if (bean.orderNo.contains(filter)) {
                        filterDepotOrderBeanList.add(bean);
                    }
                }
            }
        } else {
            if (depotOrderBeanList != null) {
                for (int i = 0; i < depotOrderBeanList.size(); i++) {
                    DOSOrderBean bean = depotOrderBeanList.get(i);
                    filterDepotOrderBeanList.add(bean);
                }
            }
        }
    }

    //查询订单服务
    class QueryDepotOrderTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        boolean mdepotChanged = false;

        QueryDepotOrderTask(Context context, boolean depotChanged) {
            super();
            mcontext = context;
            mdepotChanged = depotChanged;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                //查询各tab页面数量
                depotOrderStatis = WebServiceHelper_DepotOutSale.DEPOT_OUT_SALE_01();
                //查询列表数据
                depotOrderBeanList = WebServiceHelper_DepotOutSale.DEPOT_OUT_SALE_02(bizId);
                msg = "成功";

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                filterData(et_bar.getUpperText().toString());
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //绑定订单数据
    private void BindData(Context context) {
        //标题
        bt_undo.setText("未处理[" + depotOrderStatis.undoOrderCount + "]");
        bt_done.setText("已完成[" + depotOrderStatis.doneOrderCount + "]");
        //数据
        DOSOrderAdapter adapter = new DOSOrderAdapter(filterDepotOrderBeanList, context);
        lv_data.setAdapter(adapter);
    }

    private  class DOSOrderAdapter extends BaseAdapter {
        private List<DOSOrderBean> depotOrderBeanList;//数据源
        private LayoutInflater mInflater;//布局装载器对象
        private Context mContext;

        public DOSOrderAdapter(List<DOSOrderBean> depotOrderBeanList, Context context) {
            this.depotOrderBeanList = depotOrderBeanList;
            mInflater = LayoutInflater.from(context);
            mContext = context;
        }
        @Override
        public int getCount() {
            //return返回的是int类型，也就是页面要显示的数量。
            return depotOrderBeanList.size();
        }
        @Override
        public Object getItem(int position) {
            return depotOrderBeanList.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //将布局文件转化为View对象
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_dosorder,null);
            }
            //拣货单号
            TextView tv_no = convertView.findViewById(R.id.tv_no);
            //时间
            TextView tv_optime = convertView.findViewById(R.id.tv_optime);
            //订单信息
            TextView tv_orderInfo = convertView.findViewById(R.id.tv_orderInfo);
            //操作员
            TextView tv_operator = convertView.findViewById(R.id.tv_operator);
            //仓库
            TextView tv_depotName = convertView.findViewById(R.id.tv_depotName);
            //金额
            TextView tv_amount = convertView.findViewById(R.id.tv_amount);

            //获取相应索引的ItemBean对象
            DOSOrderBean bean = depotOrderBeanList.get(position);

            //设置控件的对应属性值
            tv_no.setText(bean.orderNo);
            tv_optime.setText(bean.optime);
            tv_orderInfo.setText(bean.orderInfo);
            tv_amount.setText(bean.amount);
            tv_operator.setText(bean.operator);
            tv_depotName.setText(bean.depotName);

            return convertView;
        }
    }

}
